package com.zch.string_;

@SuppressWarnings({"all"})
public class StringMethod {
    public static void main(String[] args) {

        //equals，String类中进行了重写，比较两个字符串每一位是否相等，有大小写的区分
        String s1 = "zch";
        System.out.println(s1.equals("zch"));   //true

        System.out.println(s1.equalsIgnoreCase("ZCh"));   //true

        System.out.println(s1.length());  //3

        //indexOf返回第一次索引到的字符/字符串的下标
        System.out.println(s1.indexOf('s'));  //-1

        /*
            subString(int n) 返回从n下标开始的字符串，直至末尾
            subString(int begin,int end) 返回下标从begin到end-1的字符串，左闭右开
         */
        System.out.println(s1.substring(2));   //h
        System.out.println(s1.substring(0,2));  //zc

        System.out.println(s1.toUpperCase());

        System.out.println(s1.toLowerCase());

        System.out.println("=====================");
        char[] c1 = s1.toCharArray();     //需要新建一个char型数组进行接收
        for (int i = 0; i < c1.length; i++) {
            System.out.println(c1[i]);
        }

        System.out.println("=====================");
        String ss = "薛宝钗和林黛玉和zch";
        String ss1=ss.replace("薛宝钗","林黛玉");
        System.out.println(ss1); //返回替换后的新字符串，但原字符串不变

        System.out.println("=====================");
        String name = "zch";
        int age = 20;
        double salary = 150000/12;
        char c='a';
        //%s替代字符串 %d替代整型  %f替代浮点型 %c替代char型
        String strFormat = "我叫%s,今年%d岁,月收入%.2f,代号%c";
        String ss4=String.format(strFormat,name,age,salary,c);
        System.out.println(ss4);

        System.out.println("=====================");
        String s2="你好,我是,zch";
        String[] s3=s2.split(",");
        for (int i = 0; i < s3.length; i++) {
            System.out.println(s3[i]);
        }
        //注意在分割特殊符号时，前面要加转义字符\
        String s4 = "E:\\c\\a.txt";
        String[] s5 = s4.split("\\\\");
        for (int i = 0; i < s5.length; i++) {
            System.out.println(s5[i]);
        }

        System.out.println("=====================");
        String ss2 = "just";
        String ss3 = "just";
        System.out.println(ss2.compareTo(ss3));   //长度相等，且完全匹配，返回0
        ss3 = "jutt";
        System.out.println(ss2.compareTo(ss3));   //长度相等或不等，遇到不等的字符时，返回ascii码差值
        ss3 = "justtt";
        System.out.println(ss2.compareTo(ss3));   //较短的一个匹配完，且未碰到不等字符，返回长度差值
    }
}
